<template>
  <section>
    <!-- 工具栏 -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.AR_number" placeholder="区域编号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.AR_name" placeholder="区域名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" v-on:click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 操作栏位 -->
    <el-col>
      <el-form :inline="true">
        <el-form-item>
          <el-button type="primary" @click="newFormVisible = true" size="small">新增</el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="success" @click="outExcel" size="small">导出EXCEL</el-button>
        </el-form-item>-->
      </el-form>
    </el-col>

    <!-- 展示界面 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="selsChange"
      v-loading="Loading"
    >
      <el-table-column align="center" type="selection" fixed width="50"></el-table-column>
      <el-table-column align="center" label="序号" fixed type="index" width="50"></el-table-column>
      <el-table-column align="center" label="区域名称" prop="AR_name" fixed min-width="200"></el-table-column>
      <el-table-column align="center" label="区域编号" prop="AR_number" fixed min-width="150"></el-table-column>
      <el-table-column align="center" label="地理位置" prop="AR_location" min-width="150"></el-table-column>
      <el-table-column align="center" label="创建人员" prop="AR_createman" min-width="150"></el-table-column>
      <el-table-column align="center" label="创建时间" prop="AR_date" min-width="150"></el-table-column>
      <el-table-column align="center" label="备注" prop="AR_remarks" min-width="250"></el-table-column>
      <el-table-column align="center" label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$AR_name,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :total="totalPage"
        style="float:right;"
      ></el-pagination>
    </el-col>
    <!-- 新增界面 -->
    <el-dialog title="新增区域" :visible.sync="newFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="newForm" :model="area_insert" :rules="rule">
        <el-form-item label="区域名称" prop="AR_name">
          <el-input v-model="area_insert.AR_name"></el-input>
        </el-form-item>
        <el-form-item label="地理位置" prop="AR_location">
          <el-input v-model="area_insert.AR_location"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="area_insert.AR_remarks"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="newFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="newSubmit('newForm')" :loading="newLoading">提交</el-button>
      </div>
    </el-dialog>
    <!-- 编辑界面 -->
    <el-dialog title="编辑" :visible.sync="alterFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="editForm" :model="area_update" :rules="rule">
        <el-form-item label="区域名称" prop="AR_name">
          <el-input v-model="area_update.AR_name"></el-input>
        </el-form-item>
        <el-form-item label="区域编号">
          <el-input v-model="area_update.AR_number" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="地理位置" prop="SSL_location">
          <el-input v-model="area_update.AR_location"></el-input>
        </el-form-item>
        <el-form-item label="创建人员">
          <el-input v-model="area_update.AR_createman" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-input v-model="area_update.AR_date" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="area_update.AR_remarks"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="alterFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit('editForm')" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import {
  area_select,
  area_insert,
  area_update,
  area_delete,
} from "../../api/area.js";
export default {
  data() {
    return {
      filters: {
        AR_number: "",
        AR_name: "",
        page: 1,
      },
      tableData: [
        {
          AR_name: null,
          AR_number: null,
          AR_location: null,
          AR_createman: null,
          AR_date: null,
          AR_remarks: null,
        },
      ],
      area_insert: {
        AR_name: null,
        AR_location: null,
        AR_createman: null,
        AR_remarks: null,
      },
      area_update: {
        AR_name: null,
        AR_number: null,
        AR_location: null,
        AR_createman: null,
        AR_date: null,
        AR_remarks: null,
      },
      area_delete: {
        AR_number: null,
      },
      rule: {
        AR_name: [{ required: true, message: "请输入区域", trigger: "blur" }],
        AR_location: [
          { required: true, message: "请输入地理位置", trigger: "blur" },
        ],
      },
      newFormVisible: false,
      alterFormVisible: false,
      Loading: false,
      currentPage: 1,
      totalPage: 0,
      editLoading: false,
      newLoading: false,
    };
  },
  methods: {
    // 获取数据
    getData() {
      this.Loading = true;
      area_select(this.filters).then((res) => {
        this.Loading = false;
        if (res) {
          this.tableData = res.data;
          this.totalPage = res.page;
        } else {
          this.tableData = [];
          this.totalPage = 0;
          this.currentPage = 1;
        }
      });
    },
    // 查询
    search() {
      this.filters.page = 1;
      this.getData();
    },
    // 分页
    handleCurrentChange(val) {
      this.filters.page = val === undefined ? this.currentPage : val;
      this.getData();
    },
    // 点击修改
    handleEdit(index, row) {
      this.alterFormVisible = true;
      this.area_update = Object.assign({}, row);
    },
    // 确认修改
    editSubmit(forName) {
      this.$refs[forName].validate((val) => {
        if (val) {
          this.editLoading = true;
          area_update(this.area_update).then((res) => {
            this.editLoading = false;
            if (res) {
              this.alterFormVisible = false;
              this.$alert(res.msg, "操作提示", {
                confirmButtonTextL: "确定",
                callback: (action) => {
                  this.getData();
                },
              });
            } else {
              // if (res) {
            }
          });
        }
      });
    },
    // 新增
    newSubmit(forName) {
      this.$refs[forName].validate((val) => {
        if (val) {
          this.newLoading = true;
          // this.area_insert.AR_createman = JSON.parse(
          //   sessionStorage.getItem("user")
          // ).user;
          var user = sessionStorage.getItem("user");
          this.area_insert.AR_createman = user;
          area_insert(this.area_insert).then((res) => {
            this.newLoading = false;
            if (res) {
              this.$alert(res.msg, "操作提示", {
                confirmButtonTextL: "确定",
                callback: (action) => {
                  this.getData();
                  this.newFormVisible = false;
                  this.area_insert = {
                    AR_name: null,
                    AR_location: null,
                    AR_createman: null,
                    AR_remarks: null,
                  };
                },
              });
            }
          });
        }
      });
    },
    // handleClose(done) {
    //   this.$confirm("确认关闭？")
    //     .then(_ => {
    //       done();
    //     })
    //     .catch(_ => {});
    // },
    selsChange: function (sels) {
      this.sels = sels;
    },
    handleDel(AR_name, row) {
      let AR_number = row.AR_number;
      // 提示是否删除
      this.$confirm("此操作将永久删除该区域下所有内容, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 进行删除操作
          this.Loading = true;
          area_delete(AR_number).then((res) => {
            this.Loading = false;
            // this.getData();
            if (res) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // TODO: 导出
    outExcel() {
      window.location.href =
        "http://192.168.10.252:80/dormitoryA/building/Building/areaInfoExcelOut";
    },
  },
  mounted() {
    this.search();
  },
};
</script>
<style></style>
